// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AsyncEventPatternExtensions.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the AsyncEventTaskFactoryExtensions type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Threading.EventPattern
{
    using System;
    using System.ComponentModel;

    public static class AsyncEventPatternExtensions
    {
        private static ITask FromBinding(ITaskBinding binding, TaskScheduler scheduler)
        {
            return new Task(binding, scheduler);
        }

        private static ITask<TReturn> FromBinding<TReturn>(ITaskBinding<TReturn> binding, TaskScheduler scheduler)
        {
            return new Task<TReturn>(binding, scheduler);
        }

        public static ITask FromAsyncEvent<TArg1, TArg2, TArg3>(this TaskFactory instance, Action<TArg1, TArg2, TArg3, object> action,
            TArg1 arg1, TArg2 arg2, TArg3 arg3)
        {
            return FromBinding(AsyncEventTaskBinding.From(action, arg1, arg2, arg3),
                instance.Scheduler);
        }

        public static ITask FromAsyncEvent<TArg1, TArg2>(this TaskFactory instance, Action<TArg1, TArg2, object> action, TArg1 arg1,
            TArg2 arg2)
        {
            return FromBinding(AsyncEventTaskBinding.From(action, arg1, arg2),
                instance.Scheduler);
        }

        public static ITask FromAsyncEvent<TArg1>(this TaskFactory instance, Action<TArg1, object> action, TArg1 arg1)
        {
            return FromBinding(AsyncEventTaskBinding.From(action, arg1),
                instance.Scheduler);
        }

        public static ITask FromAsyncEvent(this TaskFactory instance, Action<object> action)
        {
            return FromBinding(AsyncEventTaskBinding.From(action), instance.Scheduler);
        }

        public static ITask<TReturn> FromAsyncEvent<TEventArgs, TReturn>(this TaskFactory instance,
            Action<object> action, Func<TEventArgs, TReturn> selector) where TEventArgs : AsyncCompletedEventArgs
        {
            return FromBinding<TReturn>(AsyncEventTaskBinding.From(action, selector),
                instance.Scheduler);
        }

        public static ITask<TReturn> FromAsyncEvent<TArg1, TEventArgs, TReturn>(this TaskFactory instance,
            Action<TArg1, object> action, TArg1 arg1, Func<TEventArgs, TReturn> selector) where TEventArgs : AsyncCompletedEventArgs
        {
            return FromBinding<TReturn>(AsyncEventTaskBinding.From(action, arg1, selector),
                instance.Scheduler);
        }

        public static ITask<TReturn> FromAsyncEvent<TArg1, TArg2, TEventArgs, TReturn>(this TaskFactory instance,
            Action<TArg1, TArg2, object> action, TArg1 arg1, TArg2 arg2, Func<TEventArgs, TReturn> selector)
            where TEventArgs : AsyncCompletedEventArgs
        {
            return FromBinding<TReturn>(AsyncEventTaskBinding.From(action, arg1, arg2, selector),
                instance.Scheduler);
        }

        public static ITask<TReturn> FromAsyncEvent<TArg1, TArg2, TArg3, TEventArgs, TReturn>(this TaskFactory instance,
            Action<TArg1, TArg2, TArg3, object> action, TArg1 arg1, TArg2 arg2, TArg3 arg3, Func<TEventArgs, TReturn> selector)
            where TEventArgs : AsyncCompletedEventArgs
        {
            return FromBinding<TReturn>(AsyncEventTaskBinding.From(action, arg1, arg2, arg3, selector),
                instance.Scheduler);
        }
    }
}